<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require_once(APPCOMMON.'libraries/Smarty/Smarty.class.php');

class Ci_smarty extends Smarty{
	
	protected $_config = array();
	
	public function __construct($params = array()){
		parent::__construct();
		$file_path = APPCOMMON.'config/smarty.php';
		if(is_file($file_path)){
			require($file_path);
			$this->_config = $config;
		}else{
			echo 'The smarty configuration file does not exist.';
			exit;
		}
		$this->init($params);
		unset($config);
	}
	
	public function init($params = array()){
		$option = $this->_config;
		if($params){
			foreach($params as $key => $value){
				$option[$key] = $value;
			}
		}
		$this->setTemplateDir($option['template_dir']);
		$this->setCompileDir($option['compile_dir']);
		$this->setCacheDir($option['cache_dir']);
		$this->setConfigDir($option['config_dir']);
		$this->cache_lifetime  = $option['cache_lifetime'];
		$this->caching         = $option['caching'];
		$this->use_sub_dirs    = $option['use_sub_dirs'];
		$this->left_delimiter  = $option['left_delimiter'];
		$this->right_delimiter = $option['right_delimiter'];
	}

	// 扩展默认目录
	public function dir_extend($dirname = ''){
		if(!$dirname){
			echo 'The params: $dirname cannot empty.';
			exit;
		}
		$this->init(array(
			'template_dir' => SMARTY_TPL.$dirname,
			'compile_dir' => SMARTY_TPC.$dirname,
			'cache_dir' => SMARTY_CACHE.$dirname,
			'config_dir' => SMARTY_CONFIG.$dirname
		));
	}
	
}